home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / ed.c < prev    next >
C/C++ Source or Header  |  1993-05-14  |  21KB  |  770 lines

  1.  
  2.        /**********************************************
  3.        *
  4.        *   file d:\cips\ed.c
  5.        *
  6.        *   Functions: This file contains
  7.        *      erosion
  8.        *      dilation
  9.        *      mask_erosion
  10.        *      mask_dilation
  11.        *      interior_outline
  12.        *      exterior_outline
  13.        *      copy_3_x_3
  14.        *      opening
  15.        *      closing
  16.        *      get_shape_options
  17.        *
  18.        *   Purpose:
  19.        *      These functions perform the erosion,
  20.        *      dilation, outlining, opening and
  21.        *      closing operations.
  22.        *
  23.        *   External Calls:
  24.        *      wtiff.c - round_off_image_size
  25.        *                create_file_if_needed
  26.        *                write_array_into_tiff_image
  27.        *      tiff.c - read_tiff_header
  28.        *      rtiff.c - read_tiff_image
  29.        *      numcvrt.c - get_integer
  30.        *
  31.        *   Modifications:
  32.        *      14 March 1993 - created
  33.        *
  34.        ************************************************/
  35.  
  36. #include "cips.h"
  37.  
  38.  
  39.  
  40. short edmask1[3][3] = {{0, 1, 0},
  41.                        {0, 1, 0},
  42.                        {0, 1, 0}};
  43.  
  44. short edmask2[3][3] = {{0, 0, 0},
  45.                        {1, 1, 1},
  46.                        {0, 0, 0}};
  47.  
  48. short edmask3[3][3] = {{0, 1, 0},
  49.                        {1, 1, 1},
  50.                        {0, 1, 0}};
  51.  
  52. short edmask4[3][3] = {{1, 1, 1},
  53.                        {1, 1, 1},
  54.                        {1, 1, 1}};
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.      /*******************************************
  62.      *
  63.      *   mask_dilation(...
  64.      *
  65.      *   This function performs the dilation
  66.      *   operation using the erosion-dilation
  67.      *   3x3 masks given above.  It works on
  68.      *   0-value images.
  69.      *
  70.      *******************************************/
  71.  
  72. mask_dilation(in_name, out_name, the_image, out_image,
  73.               il, ie, ll, le, value, mask_type)
  74.    char   in_name[], out_name[];
  75.    int    il, ie, ll, le;
  76.    short  the_image[ROWS][COLS],
  77.           out_image[ROWS][COLS],
  78.           mask_type, value;
  79. {
  80.    int    a, b, count, i, j, k;
  81.    short  mask[3][3], max;
  82.  
  83.       /**************************************
  84.       *
  85.       *   Copy the 3x3 erosion-dilation mask
  86.       *   specified by the mask_type.
  87.       *
  88.       ***************************************/
  89.  
  90.    switch(mask_type){
  91.       case 1:
  92.          copy_3_x_3(mask, edmask1);
  93.          break;
  94.       case 2:
  95.          copy_3_x_3(mask, edmask2);
  96.          break;
  97.       case 3:
  98.          copy_3_x_3(mask, edmask3);
  99.          break;
  100.       case 4:
  101.          copy_3_x_3(mask, edmask4);
  102.          break;
  103.       default:
  104.          printf("\nInvalid mask type, using mask 4");
  105.          copy_3_x_3(mask, edmask4);
  106.          break;
  107.    }
  108.  
  109.    create_file_if_needed(in_name, out_name, out_image);
  110.  
  111.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  112.  
  113.       /***************************
  114.       *
  115.       *   Loop over image array
  116.       *
  117.       ****************************/
  118.  
  119.    printf("\n");
  120.  
  121.    for(i=1; i<ROWS-1; i++){
  122.       if( (i%10) == 0) printf("%3d", i);
  123.       for(j=1; j<COLS-1; j++){
  124.          max = 0;
  125.          for(a=-1; a<=1; a++){
  126.              for(b=-1; b<=1; b++){
  127.                 if(mask[a+1][b+1] == 1){
  128.                    if(the_image[i+a][j+b] > max)
  129.                       max = the_image[i+a][j+b];
  130.                 }  /* ends if mask == 1 */
  131.              }  /*  ends loop over b */
  132.          }  /* ends loop over a */
  133.          out_image[i][j] = max;
  134.       }  /* ends loop over j */
  135.    }  /* ends loop over i */
  136.  
  137.    fix_edges(out_image, 3);
  138.  
  139.    write_array_into_tiff_image(out_name, out_image,
  140.                                il, ie, ll, le);
  141.  
  142. }  /* ends mask_dilation */
  143.  
  144.  
  145.  
  146.  
  147.  
  148.      /*******************************************
  149.      *
  150.      *   mask_erosion(...
  151.      *
  152.      *   This function performs the erosion
  153.      *   operation using the erosion-dilation
  154.      *   3x3 masks given above.  It works on
  155.      *   0-value images.
  156.      *
  157.      *******************************************/
  158.  
  159. mask_erosion(in_name, out_name, the_image, out_image,
  160.              il, ie, ll, le, value, mask_type)
  161.    char   in_name[], out_name[];
  162.    int    il, ie, ll, le;
  163.    short  the_image[ROWS][COLS],
  164.           out_image[ROWS][COLS],
  165.           mask_type, value;
  166. {
  167.    int    a, b, count, i, j, k;
  168.    short  mask[3][3], min;
  169.  
  170.       /**************************************
  171.       *
  172.       *   Copy the 3x3 erosion-dilation mask
  173.       *   specified by the mask_type.
  174.       *
  175.       ***************************************/
  176.  
  177.    switch(mask_type){
  178.       case 1:
  179.          copy_3_x_3(mask, edmask1);
  180.          break;
  181.       case 2:
  182.          copy_3_x_3(mask, edmask2);
  183.          break;
  184.       case 3:
  185.          copy_3_x_3(mask, edmask3);
  186.          break;
  187.       case 4:
  188.          copy_3_x_3(mask, edmask4);
  189.          break;
  190.       default:
  191.          printf("\nInvalid mask type, using mask 4");
  192.          copy_3_x_3(mask, edmask4);
  193.          break;
  194.    }
  195.  
  196.    create_file_if_needed(in_name, out_name, out_image);
  197.  
  198.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  199.  
  200.       /***************************
  201.       *
  202.       *   Loop over image array
  203.       *
  204.       ****************************/
  205.  
  206.    printf("\n");
  207.  
  208.    for(i=1; i<ROWS-1; i++){
  209.       if( (i%10) == 0) printf("%3d", i);
  210.       for(j=1; j<COLS-1; j++){
  211.          min = value;
  212.          for(a=-1; a<=1; a++){
  213.              for(b=-1; b<=1; b++){
  214.                 if(mask[a+1][b+1] == 1){
  215.                    if(the_image[i+a][j+b] < min)
  216.                       min = the_image[i+a][j+b];
  217.                 }  /* ends if mask == 1 */
  218.              }  /*  ends loop over b */
  219.          }  /* ends loop over a */
  220.          out_image[i][j] = min;
  221.       }  /* ends loop over j */
  222.    }  /* ends loop over i */
  223.  
  224.    fix_edges(out_image, 3);
  225.  
  226.    write_array_into_tiff_image(out_name, out_image,
  227.                                il, ie, ll, le);
  228.  
  229. }  /* ends mask_erosion */
  230.  
  231.  
  232.  
  233.  
  234.  
  235.      /*******************************************
  236.      *
  237.      *   erosion(...
  238.      *
  239.      *   This function performs the erosion
  240.      *   operation.  If a value pixel has more
  241.      *   than the threshold number of 0
  242.      *   neighbors, you erode it by setting it
  243.      *   to 0.
  244.      *
  245.      *******************************************/
  246.  
  247. erosion(in_name, out_name, the_image, out_image,
  248.         il, ie, ll, le, value, threshold)
  249.    char   in_name[], out_name[];
  250.    int    il, ie, ll, le;
  251.    short  the_image[ROWS][COLS],
  252.           out_image[ROWS][COLS],
  253.           threshold, value;
  254. {
  255.    int    a, b, count, i, j, k;
  256.  
  257.    create_file_if_needed(in_name, out_name, out_image);
  258.  
  259.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  260.  
  261.       /***************************
  262.       *
  263.       *   Loop over image array
  264.       *
  265.       ****************************/
  266.  
  267.    for(i=0; i<ROWS; i++)
  268.       for(j=0; j<COLS; j++)
  269.          out_image[i][j] = the_image[i][j];
  270.  
  271.    printf("\n");
  272.  
  273.    for(i=1; i<ROWS-1; i++){
  274.       if( (i%10) == 0) printf("%3d", i);
  275.       for(j=1; j<COLS-1; j++){
  276.          if(the_image[i][j] == value){
  277.             count = 0;
  278.             for(a=-1; a<=1; a++){
  279.                 for(b=-1; b<=1; b++){
  280.                       if(the_image[i+a][j+b] == 0)
  281.                          count++;
  282.                 }  /*  ends loop over b */
  283.             }  /* ends loop over a */
  284.             if(count > threshold) out_image[i][j] = 0;
  285.          }  /* ends if the_image == value */
  286.       }  /* ends loop over j */
  287.    }  /* ends loop over i */
  288.  
  289.    fix_edges(out_image, 3);
  290.  
  291.    write_array_into_tiff_image(out_name, out_image,
  292.                                il, ie, ll, le);
  293.  
  294. }  /* ends erosion */
  295.  
  296.  
  297.  
  298.  
  299.  
  300.      /*******************************************
  301.      *
  302.      *   dilation(...
  303.      *
  304.      *   This function performs the dilation
  305.      *   operation.  If a 0 pixel has more than
  306.      *   threshold number of value neighbors,
  307.      *   you dilate it by setting it to value.
  308.      *
  309.      *******************************************/
  310.  
  311. dilation(in_name, out_name, the_image, out_image,
  312.